package com.csgc.camera.http

sealed class ResponseResult<out T> {
    data class Success<out T>(val value: T) : ResponseResult<T>()
    data class Failure(val exception: Exception?) : ResponseResult<Nothing>()
    data class Error(val throwable: Throwable?) : ResponseResult<Nothing>()
    data class Start(val msg:String = "") : ResponseResult<Nothing>()
}
//数据正确
inline fun <reified T> ResponseResult<T>.doSuccess(success: (T) -> Unit) {
    if (this is ResponseResult.Success) {
        success(value)
    }
}

//网络请求正确，接口返回数据有问题
inline fun <reified T> ResponseResult<T>.doFailure(fail: (Exception?) -> Unit) {
    if (this is ResponseResult.Failure) {
        fail(exception)
    }
}

//网络请求错误
inline fun <reified T> ResponseResult<T>.doError(failure: (Throwable?) -> Unit) {
    if (this is ResponseResult.Error) {
        failure(throwable)
    }
}

//接口请求之前
inline fun <reified T> ResponseResult<T>.doStart(start: (String) -> Unit) {
    if (this is ResponseResult.Start) {
        start(msg)
    }
}

